﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class TicketTypeDAO
    {
        private FilmTicketBookingSystemEntities _entity = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// List all Ticket Types of Ticket 
        /// </summary>
        /// <returns>List of Ticket type</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public List<TicketType> LoadTicketTypes()
        {
            var ticketTypes = from ticketType in _entity.TicketTypes where ticketType.IsDeleted == false select ticketType;
            return ticketTypes.ToList();
        }

        /// <summary>
        /// Load Ticket Type by Id 
        /// </summary>
        /// <param name="ticketTypeId"></param>
        /// <returns>A current Ticket type</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public TicketType SelectTicketType(int ticketTypeId)
        {
            var ticketType = _entity.TicketTypes.Find(ticketTypeId);
            return ticketType;
        }

        /// <summary>
        /// Add Ticket type to Model and return if it successed or not
        /// </summary>
        /// <param name="tType"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public int CreateTicketType(TicketType tType)
        {
            try
            {
                _entity.TicketTypes.Add(tType);
                int resultQuery = _entity.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if Ticket type name is existed or not
        /// </summary>
        /// <param name="tType"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public bool CheckIsExisted(TicketType tType)
        {
            var isExist = _entity.TicketTypes.Any(t => t.TicketTypeName == tType.TicketTypeName);
            return isExist;
        }

        /// <summary>
        /// Edit Ticket type information by input ticket type id and changing information
        /// </summary>
        /// <param name="tType"></param>
        /// <returns> true|| false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public int EditTicketType(TicketType tType)
        {
            //tType.IsDeleted = false;
            //_entity.Entry(tType).State = EntityState.Modified;
            //_entity.SaveChanges();
            //// show if save change successful or not
            //// _entity.SaveChanges() will show the number of successful entities when saving
            //if (_entity.SaveChanges() > 0)
            //{
            //    return true;
            //}
            //return false;

            try
            {
                _entity.Entry(tType).State = EntityState.Modified;
                int resultQuery = _entity.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Romove ticket type by update status isDeleted to false
        /// </summary>
        /// <param name="tTypeId"></param>
        /// <returns></returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/3</date>
        public int RemoveTicketType(TicketType tType)
        {
            try
            {
                tType.IsDeleted = true;
                _entity.Entry(tType).State = EntityState.Modified;
                int result = _entity.SaveChanges();
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}